package controller;


import dao.CommentDao;
import entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
    CommentDao commentDao = new CommentDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从session作用域获取当前的登录用户，文章作者id
        HttpSession session = request.getSession();
        User login_user = (User) session.getAttribute("login_user");
        Integer uId = login_user.getId();
        //接收参数
        String aId = request.getParameter("id");

        String comment = request.getParameter("comment");
        System.out.println(uId+", "+Integer.parseInt(aId)+","+comment);
        int total = commentDao.addComment(uId, Integer.parseInt(aId),comment);
        if (total == 1) {
            System.out.println("已成功添加" + total + "条数据");
            // 向B端提交绑定的数据
            request.setAttribute("msg", "添加成功");
            response.sendRedirect("/main");
        } else {
            System.out.println("数据添加失败");
            // 向B端携带失败数据
            request.setAttribute("msg", "添加失败");
            response.sendRedirect("/main");
        }
    }
}
